package com.neuedu.service.impl;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neuedu.config.NeueduException;
import com.neuedu.entity.PmsOrder;
import com.neuedu.entity.PmsRegist;
import com.neuedu.mapper.PmsRegistMapper;
import com.neuedu.service.PayService;
import com.neuedu.service.PmsOrderService;
import com.neuedu.service.PmsRegistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.util.PayContent;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 挂号表 服务实现类
 * </p>
 *
 * @author 王禹
 * @since 2025-08-08
 */
@Service
public class PmsRegistServiceImpl extends ServiceImpl<PmsRegistMapper, PmsRegist> implements PmsRegistService {
    @Resource
    PayService payService;
    @Resource
    PmsOrderService pmsOrderService;
    @Override
    @Transactional
    public String add(PmsRegist pmsRegist, Long openId) throws AlipayApiException, JsonProcessingException {
        pmsRegist.setCreateDate(LocalDate.now());
        pmsRegist.setCreateTime(LocalTime.now());
        pmsRegist.setUserId(openId);
        Random random = new Random(openId);
        String registerNo = "R" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + String.valueOf(random.nextInt(100000, 999999));
        pmsRegist.setRegisterNo(registerNo);
        if (this.save(pmsRegist)) {
            PmsOrder pmsOrder = new PmsOrder(null,
                    pmsRegist.getPrice(),
                    "挂号费",
                    registerNo,
                    LocalDate.now(),
                    LocalTime.now(),
                    openId,
                    (byte)1,
                    null);
            pmsOrderService.save(pmsOrder);
            PayContent payContent = new PayContent(
                    registerNo,
                    String.valueOf(pmsRegist.getPrice() / 100),
                    pmsOrder.getType(),
                    pmsOrder.getType(),
                    "FAST_INSTANT_TRADE_PAY"
            );
            return payService.pay(payContent);
        }
        throw new NeueduException("挂号失败");
    }

    @Override
    public List<PmsRegist> list(Long open_id, LocalDate registDate, Byte active) {
        QueryWrapper<PmsRegist> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", open_id);
        if (null != registDate) {
            wrapper.eq("regist_date", registDate);
        }
        if (null != active) {
            wrapper.eq("active", active);
        }
        return this.list(wrapper);
    }

    @Override
    public Boolean updatePay(String registerNo, byte active)  {
            PmsRegist pmsRegist = new PmsRegist();
            pmsRegist.setActive(active);
            UpdateWrapper<PmsRegist> wrapper = new UpdateWrapper<>();
            wrapper.eq("register_no", registerNo);
            return this.update(pmsRegist, wrapper);



    }

    @Override
    public List<PmsRegist> getRegisterByDoctorId(Long openId, Byte registTime) {
        QueryWrapper<PmsRegist> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id", openId)
                .eq("regist_date", LocalDate.now())
                .eq("regist_time", registTime)
                .eq("active", 2);
        return this.list(wrapper);
    }
}
